/*
 * Copyright (c) 2011 Ball State University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * NOTE:
 * Derivative work based on a sample project from CommonsWare, LLC
 * From _The Busy Coder's Guide to Advanced Android Development_
 *		http://commonsware.com/AdvAndroid
 */

package edu.bsu.android.gtv.apps.bconnected.ui;

import edu.bsu.android.gtv.apps.bconnected.R;
import edu.bsu.android.gtv.apps.bconnected.ui.base.BaseActivity;
import edu.bsu.android.gtv.apps.bconnected.util.UIUtils;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;

/**
 * A multi-pane activity, consisting of a {@link ConnectionFragment} and a {@link ConnectionInfoFragment} 
 * @author Kyle Parker, University Computing Services
 */
public class ConnectionMultiPaneActivity extends BaseActivity implements 
	ConnectionFragment.OnConnectionListener { 

    private FragmentManager mFragmentManager;
    private ConnectionFragment mConnectionFragment;
	private Activity mActivity;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connection);

		mActivity = this;
        getActivityHelper().setupActionBar(false);
        
        mFragmentManager = getFragmentManager();
        mConnectionFragment = (ConnectionFragment) mFragmentManager.findFragmentById(R.id.fragment_connection_list); 
        mConnectionFragment.setOnConnectionListener(this);
        mConnectionFragment.enablePersistentSelection();

        UIUtils.setTitle(mActivity, "My Connections");
    }

    /**
     * Create a fragment transaction and load the organizations for the selected category
     * loadConnectionInfo found in {@link ConnectionInfoFragment}
     * @param bsuID
     * @param personType
     */
	public void addConnectionInfoFragment(String bsuID, int personType) {
		ConnectionInfoFragment info = (ConnectionInfoFragment) mFragmentManager.findFragmentById(R.id.fragment_container_connection_info);
		FragmentTransaction ft = mFragmentManager.beginTransaction();
		
		if (info == null) {
			info = new ConnectionInfoFragment();
			
			ft.add(R.id.fragment_container_connection_info, info)
				.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
				.addToBackStack(null)
				.commit();
		}
			
		info.startActivity(bsuID, personType);
	}
	
	/**
	 * @see edu.bsu.android.apps.gtv.mybsu.ui.ConnectionFragment.OnConnectionListener#onConnectionSelected(int)
	 * When the user taps a connection, the OnConnectionListener in the ConnectionFragment will fire, calling this method
	 * The addConnectionInfoFragment method will be called, creating a fragment transaction and loading the connection info 
	 * based on the connection selected
	 */
	public void onConnectionSelected(String bsuID, int personType) { 
		addConnectionInfoFragment(bsuID, personType);
	}
}
